package com.csap.framework.oauth2.server.service.impl;

import com.csap.framework.annotation.apidoc.Description;
import com.csap.framework.oauth2.server.entity.ClientEntity;
import com.csap.framework.oauth2.server.model.ClientModel;
import com.csap.framework.oauth2.server.mybatis.ClientMapper;
import com.csap.framework.oauth2.server.service.ClientAllowUrlService;
import com.csap.framework.oauth2.server.service.ClientScopeService;
import com.csap.framework.oauth2.server.service.ClientService;
import com.csap.framework.util.Assert;
import com.csap.framework.util.optional.Optional;
import com.csap.mybatisplus.extension.service.impl.BaseCsapServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 客户端信息
 * </p>
 *
 * @author yangchengfu
 * @dateTime 2021-07-25 23:51:30
 */
@Service
@Description(value = "客户端信息ServiceImpl")
public class ClientServiceImpl extends BaseCsapServiceImpl<ClientMapper, ClientEntity, ClientModel, String> implements ClientService {
    @Resource
    private ClientScopeService scopeService;
    @Resource
    private ClientAllowUrlService allowUrlService;

    @Override
    public Optional<ClientModel> getClientModel(String clientId) {
        Assert.notEmpty(clientId, "客户端id不能为空");
        return lambdaQuery()
                .select(ClientEntity::getClientId, ClientEntity::getClientSecret)
                .eq(ClientEntity::getClientId, clientId)
                .oneOptional()
                .transformOptional(ClientModel.class);
    }

    @Override
    public Optional<ClientModel> getCheckClientModel(String clientId) {
        return getClientModel(clientId)
                .then(i -> i.setAllowUrl(allowUrlService.allowUrl(clientId)))
                .then(i -> i.setScope(scopeService.scopeList(clientId)))
                .then(i -> i.setPermissions(scopeService.permissionsList(i.getClientId())))
                .transformOptional(ClientModel.class);
    }
}
